// 提交链接 https://leetcode.cn/problems/binary-tree-maximum-path-sum/submissions/562210631/
// 124 二叉树中的最大路径和
// 完成日期： 2024.10.7
// 树状dp
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans = -10000000;
    int maxPathSum(TreeNode* root) {
        solve(root);
        return ans;
    }
    int solve(TreeNode* root)
    {
        if (!root) return 0;
        int n1 = 0, n2 = 0;
        n1 = max(n1, solve(root->left));
        n2 = max(n2, solve(root->right));
        ans = max(ans, root->val + n1 + n2);
        return root->val + max(n1, n2);
    }
};